package Model.GoogleMaps;

import java.io.IOException;
import java.io.InputStream;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Properties;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import MailGenerator.Mailer;
import DBAccess.DBAccess;

import Model.GoogleMaps.*;
import Model.DateProfile.*;
import Model.User;
import org.json.*;

/**
 * Servlet implementation class RegistrationServlet
 */
@WebServlet("/googlemap")
public class MapServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private Properties props = new Properties();

	@Override
	public void init() {
		InputStream in = this.getServletContext().getResourceAsStream(
				"/config.properties");
		try {
			props.load(in);
		} catch (Exception e) {
			System.err
					.println("Unrecoverable error. Could not load configuration.");
		}
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();

		String userIDString = request.getParameter("userID");
		// TODO:redirect if not logged
		int userID = Integer.parseInt(userIDString);
		String otherIDString = request.getParameter("otherid");
		int otherID = Integer.parseInt(otherIDString);
		String dateWhereString = request.getParameter("dateWhere");
		String button = request.getParameter("button");

		double dateWhere1 = 49.28203;
		double dateWhere2 = -123.12078;

		String dateType = request.getParameter("dateType");

		if (dateWhereString.equals("pacific_center")) {
			dateWhere1 = 49.28203;
			dateWhere2 = -123.12078;
		}

		if (dateWhereString.equals("richmond_center")) {
			dateWhere1 = 49.16325;
			dateWhere2 = -123.14005;
		}

		if (dateWhereString.equals("metrotown")) {
			dateWhere1 = 49.23108;
			dateWhere2 = -123.00742;
		}

		request.setAttribute("tryingAgain", false);

		RequestDispatcher dispatcher;

		if (button.equals("Find Date Location!")) {
			if (dateType != null && session != null) {
				session.setAttribute("dateLoc", dateType);
				session.setAttribute("dateLat", dateWhere1);
				session.setAttribute("dateLng", dateWhere2);

				String originalTarget = (String) session
						.getAttribute("originalTarget");
				if (originalTarget != null) {
					response.sendRedirect(originalTarget);
					return;
				}

				
				dispatcher = request.getRequestDispatcher("/googleMapDate.jsp?id="+otherID);
			} else {
				request.setAttribute("tryingAgain", true);
				dispatcher = request.getRequestDispatcher("/googleMapDate.jsp?id="+otherID);
			}
			request.setAttribute("MapResult", Boolean.TRUE);
			dispatcher.forward(request, response);
		}

		if (button.equals("Set up a date!")) {
			new DBAccess();
			Statement st = null;
			ResultSet rs = null;
			try {
				st = DBAccess.con.createStatement();
				rs = st.executeQuery("SELECT name FROM accountdb where userID='"
						+ userID + "'");
				rs.next();
				String myName = rs.getString(1);
				rs = st.executeQuery("SELECT email FROM accountdb where userID='"
						+ otherID + "'");
				rs.next();
				String dateEmail = rs.getString(1);
				System.out.println(dateEmail);
				System.out.println(myName);
				System.out.println(dateType);
				Mailer.sendMail(dateEmail, myName
						+ " has sent you a date request!", myName
						+ " wants to meet you for " + dateType);
				
				
				String referer = request.getHeader("Referer");
				referer = "http://localhost:8080/UBCDatingServer/profile.jsp";
				// dispatcher = request.getRequestDispatcher(referer);
				response.sendRedirect(referer);
			}catch (Exception e) {
				e.printStackTrace();
			}

			DBAccess.close(st, rs);
		}

	}
}
